```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>深入解析ConcurrentHashMap的Segment扩容机制</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .code-block {
            background-color: #282c34;
            border-radius: 8px;
            padding: 1.5rem;
            overflow-x: auto;
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
        }
        .section-divider {
            height: 1px;
            background: linear-gradient(90deg, rgba(0,0,0,0) 0%, rgba(118,75,162,0.5) 50%, rgba(0,0,0,0) 100%);
        }
        .drop-cap::first-letter {
            float: left;
            font-size: 4.5rem;
            line-height: 0.65;
            margin: 0.1em 0.2em 0 0;
            color: #667eea;
            font-weight: 700;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:px-0">
        <div class="container mx-auto max-w-5xl">
            <div class="flex flex-col items-center justify-center text-center">
                <h1 class="text-4xl md:text-6xl font-bold mb-6 font-serif">深入解析ConcurrentHashMap的Segment扩容机制</h1>
                <p class="text-xl md:text-2xl max-w-3xl opacity-90 mb-8">Java 7并发编程中高效哈希表的核心实现原理</p>
                <div class="flex items-center space-x-4">
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-code mr-2"></i>Java</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-cogs mr-2"></i>并发编程</span>
                    <span class="px-4 py-2 bg-white bg-opacity-20 rounded-full"><i class="fas fa-chart-line mr-2"></i>性能优化</span>
                </div>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 md:px-0 py-12">
        <!-- Introduction -->
        <article class="mb-16">
            <p class="drop-cap text-gray-700 leading-relaxed text-lg mb-6">在Java 7及之前的版本中，ConcurrentHashMap使用Segment来实现并发控制。每个Segment实际上是一个独立的哈希表，并且每个Segment使用独立的锁来控制对该段的并发访问。扩容操作是ConcurrentHashMap中一个重要的性能优化部分，它在处理大量数据时会影响性能和内存使用。</p>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 my-6">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <svg class="h-5 w-5 text-blue-500" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"></path>
                        </svg>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-blue-700">关键概念：Segment是ConcurrentHashMap在Java 7中的并发控制单元，它将哈希表分成多个独立的段(segment)，每个段有自己的锁。这种设计允许多个线程同时访问不同的段，从而实现高并发。</p>
                    </div>
                </div>
            </div>
        </article>

        <!-- Section 1 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-purple-600 text-white flex items-center justify-center mr-4">
                    <span class="font-bold">1</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">Segment扩容的基本原理</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-purple-100 text-purple-600 flex items-center justify-center mr-4">
                            <i class="fas fa-bolt"></i>
                        </div>
                        <h3 class="text-xl font-semibold">扩容触发</h3>
                    </div>
                    <p class="text-gray-700">当Segment中的元素数量达到一定的阈值时（通常是负载因子与容量的乘积），会触发扩容操作。这个阈值保证了哈希表在容量和性能之间取得平衡。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-blue-100 text-blue-600 flex items-center justify-center mr-4">
                            <i class="fas fa-bullseye"></i>
                        </div>
                        <h3 class="text-xl font-semibold">扩容目标</h3>
                    </div>
                    <p class="text-gray-700">扩容的主要目标是增加Segment的容量，以减少哈希冲突和提高性能。更大的容量意味着更分散的元素分布，从而减少链表长度和查找时间。</p>
                </div>
            </div>
            
            <div class="mt-8 bg-white p-6 rounded-xl shadow-md">
                <div class="mermaid">
                    graph TD
                        A[元素插入Segment] --> B{元素数量 > 阈值?}
                        B -->|是| C[触发扩容]
                        B -->|否| D[正常插入]
                        C --> E[计算新容量]
                        E --> F[创建新哈希表]
                        F --> G[重新分配元素]
                        G --> H[更新扩容状态]
                </div>
            </div>
        </section>

        <div class="section-divider my-12"></div>

        <!-- Section 2 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-purple-600 text-white flex items-center justify-center mr-4">
                    <span class="font-bold">2</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">扩容流程</h2>
            </div>
            
            <!-- Step 2.1 -->
            <div class="mb-12">
                <div class="flex items-center mb-4">
                    <div class="w-6 h-6 rounded-full bg-gray-200 flex items-center justify-center mr-4">
                        <span class="text-sm font-bold">2.1</span>
                    </div>
                    <h3 class="text-2xl font-semibold">计算新容量</h3>
                </div>
                <p class="text-gray-700 mb-6">扩容时，首先计算新的容量。新的容量通常是当前容量的两倍，这种指数级增长策略确保有足够的空间来存储新插入的元素，同时避免频繁扩容。</p>
                
                <div class="code-block text-gray-200">
                    <pre><code>private void resize() {
    int newCapacity = table.length * 2;
    rehash(newCapacity);
}</code></pre>
                </div>
            </div>
            
            <!-- Step 2.2 -->
            <div class="mb-12">
                <div class="flex items-center mb-4">
                    <div class="w-6 h-6 rounded-full bg-gray-200 flex items-center justify-center mr-4">
                        <span class="text-sm font-bold">2.2</span>
                    </div>
                    <h3 class="text-2xl font-semibold">创建新的哈希表</h3>
                </div>
                <p class="text-gray-700 mb-6">创建一个新的更大的数组来存储扩容后的数据。新数组的长度是原数组的两倍，这种扩容方式在时间和空间复杂度之间取得了良好的平衡。</p>
                
                <div class="code-block text-gray-200">
                    <pre><code>private void rehash(int newCapacity) {
    Node&lt;K,V&gt;[] newTable = new Node[newCapacity];
    // 重新计算每个元素的位置并插入新表
    transfer(newTable);
    table = newTable;
}</code></pre>
                </div>
            </div>
            
            <!-- Step 2.3 -->
            <div class="mb-12">
                <div class="flex items-center mb-4">
                    <div class="w-6 h-6 rounded-full bg-gray-200 flex items-center justify-center mr-4">
                        <span class="text-sm font-bold">2.3</span>
                    </div>
                    <h3 class="text-2xl font-semibold">重新分配和迁移元素</h3>
                </div>
                <p class="text-gray-700 mb-6">在新的哈希表中重新分配现有元素。这个过程涉及遍历旧的Segment中的所有桶，将每个桶中的元素重新哈希到新的位置，确保元素分布均匀。</p>
                
                <div class="code-block text-gray-200">
                    <pre><code>private void transfer(Node&lt;K,V&gt;[] newTable) {
    Node&lt;K,V&gt;[] oldTable = table;
    int oldCapacity = oldTable.length;
    for (int i = 0; i &lt; oldCapacity; i++) {
        Node&lt;K,V&gt; head = oldTable[i];
        if (head != null) {
            // 迁移节点到新表
            Node&lt;K,V&gt; next;
            do {
                next = head.next;
                int index = (head.hash & (newTable.length - 1));
                head.next = newTable[index];
                newTable[index] = head;
                head = next;
            } while (head != null);
        }
    }
}</code></pre>
                </div>
                
                <div class="mt-6 bg-white p-6 rounded-xl shadow-md">
                    <div class="mermaid">
                        sequenceDiagram
                            participant Thread as 工作线程
                            participant Segment as Segment
                            participant OldTable as 旧哈希表
                            participant NewTable as 新哈希表
                            
                            Thread->>Segment: 调用resize()
                            Segment->>Segment: 计算新容量 (2倍)
                            Segment->>NewTable: 创建新哈希表
                            loop 迁移每个桶
                                Thread->>OldTable: 获取桶i的头节点
                                OldTable-->>Thread: 返回head节点
                                loop 遍历链表
                                    Thread->>Thread: 计算新位置 (head.hash & (newSize-1))
                                    Thread->>NewTable: 插入节点到新位置
                                    Thread->>Thread: 移动到next节点
                                end
                            end
                            Thread->>Segment: 更新table引用
                    </div>
                </div>
            </div>
            
            <!-- Step 2.4 -->
            <div class="mb-12">
                <div class="flex items-center mb-4">
                    <div class="w-6 h-6 rounded-full bg-gray-200 flex items-center justify-center mr-4">
                        <span class="text-sm font-bold">2.4</span>
                    </div>
                    <h3 class="text-2xl font-semibold">更新扩容状态</h3>
                </div>
                <p class="text-gray-700">更新扩容状态，设置新的哈希表为当前使用的表，并确保新表的容量和其他相关属性得到正确设置。这个过程完成后，所有新操作都将使用扩容后的新表。</p>
            </div>
        </section>

        <div class="section-divider my-12"></div>

        <!-- Section 3 -->
        <section class="mb-16">
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-purple-600 text-white flex items-center justify-center mr-4">
                    <span class="font-bold">3</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">锁机制的扩容</h2>
            </div>
            
            <div class="bg-white p-6 rounded-xl shadow-md card-hover mb-8">
                <div class="flex items-center mb-4">
                    <div class="w-10 h-10 rounded-full bg-green-100 text-green-600 flex items-center justify-center mr-4">
                        <i class="fas fa-lock"></i>
                    </div>
                    <h3 class="text-xl font-semibold">分段锁定</h3>
                </div>
                <p class="text-gray-700 mb-4">在ConcurrentHashMap的扩容过程中，Segment自身会进行分段锁定，确保在扩容期间对Segment的其他操作不会受到影响。这种设计允许其他线程继续访问其他未锁定的Segment，保证了高并发性能。</p>
                
                <div class="code-block text-gray-200">
                    <pre><code>// 扩容期间，锁定分段
synchronized (segmentLock) {
    // 执行扩容操作
}</code></pre>
                </div>
            </div>
            
            <div class="bg-yellow-50 border-l-4 border-yellow-400 p-4">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <svg class="h-5 w-5 text-yellow-500" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z" clip-rule="evenodd"></path>
                        </svg>
                    </div>
                    <div class="ml-3">
                        <p class="text-sm text-yellow-700">注意：虽然锁定了正在扩容的Segment，但其他Segment仍然可以被其他线程并发访问。这是ConcurrentHashMap高并发性能的关键设计之一。</p>
                    </div>
                </div>
            </div>
        </section>

        <div class="section-divider my-12"></div>

        <!-- Section 4 -->
        <section>
            <div class="flex items-center mb-8">
                <div class="w-8 h-8 rounded-full bg-purple-600 text-white flex items-center justify-center mr-4">
                    <span class="font-bold">4</span>
                </div>
                <h2 class="text-3xl font-bold text-gray-800">扩容对性能的影响</h2>
            </div>
            
            <div class="grid md:grid-cols-2 gap-8 mb-8">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-red-100 text-red-600 flex items-center justify-center mr-4">
                            <i class="fas fa-tachometer-alt"></i>
                        </div>
                        <h3 class="text-xl font-semibold">性能优化</h3>
                    </div>
                    <p class="text-gray-700">扩容操作会在扩容过程中锁定Segment，从而避免了其他线程对该Segment的操作。这种细粒度锁设计允许高并发访问，同时进行扩容，尽可能减少对整体性能的影响。</p>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="flex items-center mb-4">
                        <div class="w-10 h-10 rounded-full bg-indigo-100 text-indigo-600 flex items-center justify-center mr-4">
                            <i class="fas fa-memory"></i>
                        </div>
                        <h3 class="text-xl font-semibold">内存使用</h3>
                    </div>
                    <p class="text-gray-700">扩容时需要额外的内存来存储新的哈希表，但这有助于减少哈希冲突，提高查找效率。扩容完成后，旧表会被垃圾回收器回收，内存使用会趋于稳定。</p>
                </div>
            </div>
            
            <div class="bg-white p-6 rounded-xl shadow-md">
                <h3 class="text-xl font-semibold mb-4">Java 7 vs Java 8+ ConcurrentHashMap对比</h3>
                <div class="overflow-x-auto">
                    <table class="min-w-full divide-y divide-gray-200">
                        <thead class="bg-gray-50">
                            <tr>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">特性</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Java 7 ConcurrentHashMap</th>
                                <th scope="col" class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">Java 8+ ConcurrentHashMap</th>
                            </tr>
                        </thead>
                        <tbody class="bg-white divide-y divide-gray-200">
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">并发控制</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">Segment分段锁</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">CAS + synchronized</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">扩容单位</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">Segment级别</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">桶级别</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">哈希冲突处理</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">链表</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">链表+红黑树</td>
                            </tr>
                            <tr>
                                <td class="px-6 py-4 whitespace-nowrap text-sm font-medium text-gray-900">并发度</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">固定(构造时指定)</td>
                                <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">动态调整</td>
                            </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </section>
    </main>

    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto max-w-5xl px-4 md:px-0">
            <div class="flex flex-col items-center">
                <div class="mb-4">
                    <span class="text-xl font-semibold text-white">技术小馆</span>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-gray-400 hover:text-white transition-colors duration-300">
                        <i class="fas fa-link mr-2"></i>http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
        </div>
    </footer>

    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            },
            sequence: {
                diagramMarginX: 50,
                diagramMarginY: 10,
                boxTextMargin: 5,
                noteMargin: 10,
                messageMargin: 35,
                mirrorActors: true
            }
        });
    </script>
</body>
</html>
```